package com.adingxiong.cft.aop;

import com.adingxiong.cft.annotate.Mylog;
import com.adingxiong.cft.annotate.StrCut;
import com.adingxiong.cft.entity.SysLog;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.List;

/**
 * @ClassName StrCutAspect
 * @Description TODO
 * @Author xiongchao
 * @Date 2021/3/9 16:36
 **/
@Aspect
@Component
public class StrCutAspect {

    @Pointcut("@annotation(com.adingxiong.cft.annotate.StrCut)")
    public void cutPoint(){

    }

    @AfterReturning(returning = "data",pointcut = "cutPoint()")
    public Object afterRun (JoinPoint joinPoint,Object data){
        System.out.println(data);
//从切面织入点处通过反射机制获取织入点处的方法
        MethodSignature signature = (MethodSignature) joinPoint.getSignature();
        //获取切入点所在的方法
        Method method = signature.getMethod();
        StrCut strCut = method.getAnnotation(StrCut.class);
        Page<SysLog> res = (Page<SysLog>) data;
        List<SysLog> records = res.getRecords();
        if(!CollectionUtils.isEmpty(records)){
            records.stream().forEach(e -> {
                e.setMethodName(e.getMethodName().substring(0,strCut.length() +1));
            });
        }
        res.setRecords(records);
        return res;
    }
}
